﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using TalksLog.Domain.Core;
using TalksLog.Domain.DomainEvents;

namespace TalksLog.Infrastructure.Email
{
    public class EmailSender : IDomainEventHandle<CreateEmailVerificationEvent>
    {
        public void Handle(CreateEmailVerificationEvent domainEvent)
        {
            string email = domainEvent.Email;
            string subject = "邮箱验证";
            string context = $"请访问以下链接完成验证：http://www.talkslog.cn/findingpassword/verifyemail?&email={HttpUtility.UrlEncode(domainEvent.Email)}&code={HttpUtility.UrlEncode(domainEvent.VerificationCode)}";

            this.SendEmail(email, subject, context);
        }

        public void SendEmail(string email, string subject, string content)
        {
            MailMessage message = new MailMessage();

            message.From = new MailAddress("zzy_0471@163.com", "TalksLog.CN");
            message.To.Add(new MailAddress(email));
            message.Subject = subject;
            message.Body = content;

            SmtpClient client = new SmtpClient("smtp.163.com");
            client.Credentials = new NetworkCredential("zzy_0471@163.com", "274Pa$$w0rd");

            client.Send(message);
        }
    }
}
